package com.ccsoft.atools.controller;

import com.ccsoft.atools.model.SysUserRole;
import com.ccsoft.atools.service.SysUserRoleService;
import com.ccsoft.atools.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/sysUserRole")
public class SysUserRoleController {

    @Autowired
    private SysUserRoleService service;

    @DeleteMapping("/deleteByPrimaryKey/{iid}")
    public JsonResult deleteByPrimaryKey(@PathVariable java.lang.Integer iid) {
        return JsonResult.ok(service.deleteByPrimaryKey(iid));
    }

    @GetMapping("/selectByPrimaryKey/{iid}")
    public JsonResult selectByPrimaryKey(@PathVariable java.lang.Integer iid) {
        return JsonResult.ok(service.selectByPrimaryKey(iid));
    }

    @GetMapping("/selectAll")
    public JsonResult selectAll() {
        return JsonResult.ok(service.selectAll());
    }


    @PostMapping("/insert")
    public JsonResult insert(@RequestBody SysUserRole record) {
        return JsonResult.ok(service.insertSelective(record));
    }

    @PutMapping("/update")
    public JsonResult update(@RequestBody SysUserRole record) {
        return JsonResult.ok(service.updateByPrimaryKey(record));
    }

    @GetMapping("/selectByRoleIid/{roleIid}")
    public JsonResult selectByRoleIid(@PathVariable int roleIid) {
        return JsonResult.ok(service.selectByRoleIid(roleIid));
    }

    @PostMapping("/updateRoles")
    public JsonResult updateRoles(@RequestBody Map<String,Object> params) {
        Integer userPk = (Integer) params.get("userPk");
        List<Integer> rolePks = (List<Integer>) params.get("rolePks");
        return JsonResult.ok(service.updateRoles(userPk,rolePks));
    }

}